package com.softstao.guoyu.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseFragment;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.agent.TeamCount;
import com.softstao.guoyu.mvp.interactor.agent.AgentInteractor;
import com.softstao.guoyu.mvp.presenter.agent.AgentPresenter;
import com.softstao.guoyu.mvp.viewer.agent.TeamCountViewer;
import com.softstao.guoyu.ui.activity.agent.AgentListActivity;
import com.softstao.guoyu.ui.activity.agent.InviteActivity;
import com.softstao.guoyu.ui.activity.agent.MyParentAgentActivity;
import com.softstao.guoyu.ui.activity.agent.AuditListActivity;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/7.
 */

public class AgentFragment extends BaseFragment implements TeamCountViewer{

    @AIPresenter(presenter = AgentPresenter.class, interactor = AgentInteractor.class)
    AgentPresenter presenter;

    @BindView(R.id.total_num)
    TextView totalNum;
    @BindView(R.id.direct_view)
    TextView directView;
    @BindView(R.id.recommend_from_me)
    TextView recommendFromMe;
    @BindView(R.id.recommend_to_me)
    TextView recommendToMe;
    private TeamCount teamCount;

    @Override
    public int _ContentView() {
        return R.layout.fragment_agent;
    }

    @Override
    public void initView() {
        setTitleBar("代理");
        titleBar.setRightText("邀请经销商");
        titleBar.setRightTextViewOnClick(v -> startActivity(new Intent(mContext, InviteActivity.class)));

    }

    private void initData() {
        totalNum.setText("我的团队人数：" + (teamCount.getDirectSubCount()+teamCount.getMeRecommendCount()+teamCount.getRecommendToMeCount()) + "人");
        directView.setText("直属代理：" + teamCount.getDirectSubCount() + "人");
        recommendFromMe.setText("我的转推荐代理：" + teamCount.getMeRecommendCount() + "人");
        recommendToMe.setText("转荐给我的代理：" + teamCount.getRecommendToMeCount() + "人");
    }

    @OnClick({R.id.parent_agent, R.id.audit, R.id.direct_view,R.id.recommend_from_me,R.id.recommend_to_me,R.id.no_order})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.parent_agent:
                startActivity(new Intent(mContext, MyParentAgentActivity.class));
                break;
            case R.id.audit:
                startActivity(new Intent(mContext, AuditListActivity.class));
                break;
            case R.id.direct_view:
                startActivity(new Intent(mContext, AgentListActivity.class).putExtra("type","direct"));
                break;
            case R.id.recommend_from_me:
                startActivity(new Intent(mContext,AgentListActivity.class).putExtra("type","from"));
                break;
            case R.id.recommend_to_me:
                startActivity(new Intent(mContext,AgentListActivity.class).putExtra("type","to"));
                break;
            case R.id.no_order:
                startActivity(new Intent(mContext,AgentListActivity.class).putExtra("type","no_order"));
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getTeamCount();
    }

    @Override
    public void getTeamCount() {
        presenter.getTeamCount(SharePreferenceManager.getInstance().getAgentId());
    }

    @Override
    public void getResult(TeamCount teamCount) {
        if (teamCount!=null){
            this.teamCount = teamCount;
            initData();
        }
    }
}
